package com.gnerv.battle.boot.project.guard.business.impl;

import com.gnerv.battle.boot.common.base.exception.BusinessException;
import com.gnerv.battle.boot.common.base.model.RestResult;
import com.gnerv.battle.boot.common.tools.command.ExecResult;
import com.gnerv.battle.boot.common.tools.command.ShellTools;
import com.gnerv.battle.boot.project.guard.business.MonitorDeployBusiness;
import com.gnerv.battle.boot.project.guard.model.dto.MonitorHostDTO;
import com.gnerv.battle.boot.project.guard.model.entity.MonitorHost;
import com.gnerv.battle.boot.project.guard.service.MonitorDeployService;
import com.gnerv.battle.boot.project.guard.service.MonitorHostService;
import com.gnerv.battle.boot.project.guard.service.MonitorProjectNodeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
@AllArgsConstructor
public class MonitorDeployBusinessImpl implements MonitorDeployBusiness {

    private final MonitorDeployService monitorDeployService;
    private final MonitorHostService monitorHostService;

    @Override
    public RestResult<Void> deployByNodeUkId(String nodeUkId) {
        // 获取链接信息
        MonitorHostDTO monitorHostDTO = monitorHostService.selectByNodeUkId(nodeUkId);
        // 用户目录
        String homePath = "";
        ExecResult execResult = ShellTools.getHomePath(monitorHostDTO.getUsername(), monitorHostDTO.getPassword(),
                monitorHostDTO.getHost(), monitorHostDTO.getPort());
        if (execResult.getStatus() == 0) {
            homePath = execResult.getStdout();
        } else {
            throw new BusinessException(execResult.getStderr());
        }
        // 创建 远程agent部署 guard_agent 目录 在这个目录下使用节点uuid创建目录 保存日志和 本地配置-选
        String remoteAgentPath = homePath + "/guard_agent";
        execResult = ShellTools.createFolders(remoteAgentPath, monitorHostDTO.getUsername(), monitorHostDTO.getPassword(),
                monitorHostDTO.getHost(), monitorHostDTO.getPort());
        if (execResult.getStatus() == 0) {
            homePath = execResult.getStdout();
        } else {
            throw new BusinessException(execResult.getStderr());
        }
//        // 获取本地agent file 需要根据远程服务器的操作系统和cpu架构获取相应的
//        File agentFile = monitorDeployService.selectAgentFile(monitorHostDTO);
//        // 上传 agent file
//        monitorDeployService.uploadAgent(monitorHostDTO, agentFile, remoteAgentPath);
//        // 创建远程节点日志目录
//        String remoteAgentNodePath = monitorDeployService.createRemoteAgentNodePath(monitorHostDTO, remoteAgentPath);
//
//        String remoteAgentPath = monitorDeployService.createRemoteAgentPath(monitorHostDTO);
//

        return null;
    }

}
